Solved Implement a singly linked list as a functional data 您所在的位置:网站首页 kotlin when else Solved Implement a singly linked list as a functional data

Solved Implement a singly linked list as a functional data

2023-03-31 08:04| 来源: 网络整理| 查看: 265

Implement a singly linked list as a functional data structure in Kotlin. As discussed in the class, the list should be a sealed class with enclosed classes to construct a list, including construction of an empty list. The following methods should be available for a singly linked list:

1. modifyListHead() method to replace the head of the list with a different element. This function accepts an input list and an input variable, and returns a list where the head of the input list has been replaced with the input variable. For example, calling modifyListHead(exList, 10) where exList = (1,2,3,4,5) would return (10,2,3,4,5).

2. dropFromList() method to drop the first n elements from the list. This function accepts an input list and a value for n, and returns a list with the first n elements dropped. For example, calling dropFromList(exList,1) where exList = (1,2,3,4,5) would return (2,3,4,5).

3. sumTwoLists() method that accepts two lists and adds corresponding elements of each list. This function accepts two input lists and returns their sum as illustrated in the example below. For two input lists (1,2,3) and (4,5,6), this function returns (5,7,9)

4. multiplyTwoLists() method that accepts two lists and multiplies corresponding elements in the same way sumTwoLists() adds elements of two arrays.

NB: Solution that I earlier got showed some errors when tried to run it. I am also sharing the solution below:

sealed class SinglyLinkedList { object Empty : SinglyLinkedList() data class Cons(val head: T, val tail: SinglyLinkedList) : SinglyLinkedList() } fun modifyListHead(list: SinglyLinkedList, newHead: T): SinglyLinkedList = when (list) { is SinglyLinkedList.Empty -> SinglyLinkedList.Empty is SinglyLinkedList.Cons -> SinglyLinkedList.Cons(newHead, list.tail) }

fun dropFromList(list: SinglyLinkedList, n: Int): SinglyLinkedList = when (list) { is SinglyLinkedList.Empty -> SinglyLinkedList.Empty is SinglyLinkedList.Cons -> if (n == 0) list else dropFromList(list.tail, n - 1) } fun sumTwoLists(list1: SinglyLinkedList, list2: SinglyLinkedList): SinglyLinkedList = when { list1 is SinglyLinkedList.Empty && list2 is SinglyLinkedList.Empty -> SinglyLinkedList.Empty list1 is SinglyLinkedList.Empty -> list2 list2 is SinglyLinkedList.Empty -> list1 else -> SinglyLinkedList.Cons(list1.head + list2.head, sumTwoLists(list1.tail, list2.tail)) }

fun multiplyTwoLists(list1: SinglyLinkedList, list2: SinglyLinkedList): SinglyLinkedList = when { list1 is SinglyLinkedList.Empty || list2 is SinglyLinkedList.Empty -> SinglyLinkedList.Empty else -> SinglyLinkedList.Cons(list1.head * list2.head, multiplyTwoLists(list1.tail, list2.tail)) }

fun main(){ val exList = linkedListOf(1, 2, 3, 4, 5) println(exList)

// Output: 1 2 3 4 5

val modifiedList = exList.modifyListHead(10) println(modifiedList)

// Output: 10 2 3 4 5

val droppedList = exList.dropFromList(1) println(droppedList)

// Output: 2 3 4 5

val list1 = linkedListOf(1, 2, 3) val list2 = linkedListOf(4, 5, 6) val sumList = list1.sumTwoLists(list2) println(sumList)

// Output: 5 7 9

val multiplyList = list1.multiplyTwoLists(list2) println(multiplyList)

// Output: 4 10 18 }

NB: Erlier I got the solution from chegg but when I tried to run the code I got the following error:

Type parameter T is declared as 'out' but occurs in 'in' position in type T

Type parameter T is declared as 'out' but occurs in 'in' position in type LinkedList

Type mismatch: inferred type is Int but T was expected

Type mismatch: inferred type is Int but T was expected

Type parameter T is declared as 'out' but occurs in 'in' position in type LinkedList

Type mismatch: inferred type is Int but T was expected

Type mismatch: inferred type is Int but T was expected

Solution that i got from chegg is given below:

sealed class LinkedList { object Empty : LinkedList() data class Node(val value: T, val next: LinkedList) : LinkedList()

fun modifyListHead(newValue: T): LinkedList { return when (this) { is Node -> Node(newValue, next) Empty -> Empty } }

fun dropFromList(n: Int): LinkedList { return when (this) { is Node -> if (n > 0) next.dropFromList(n - 1) else this Empty -> Empty } }

fun sumTwoLists(otherList: LinkedList): LinkedList { return when (this) { is Node -> when (otherList) { is Node -> Node((value as Int) + (otherList.value as Int), next.sumTwoLists(otherList.next)) Empty -> Empty } Empty -> Empty } }

fun multiplyTwoLists(otherList: LinkedList): LinkedList { return when (this) { is Node -> when (otherList) { is Node -> Node((value as Int) * (otherList.value as Int), next.multiplyTwoLists(otherList.next)) Empty -> Empty } Empty -> Empty } } }

fun List.toLinkedList(): LinkedList { return this.foldRight(LinkedList.Empty as LinkedList) { value, acc -> LinkedList.Node(value, acc) } } fun main() { val exList = listOf(1, 2, 3, 4, 5).toLinkedList()

// Test modifyListHead() val newList = exList.modifyListHead(10) println(newList) // Output: Node(value=10, next=Node(value=2, next=Node(value=3, next=Node(value=4, next=Node(value=5, next=Empty))))))

// Test dropFromList() val droppedList = exList.dropFromList(2) println(droppedList) // Output: Node(value=3, next=Node(value=4, next=Node(value=5, next=Empty))))

// Test sumTwoLists() val sumList = exList.sumTwoLists(listOf(4, 5, 6).toLinkedList()) println(sumList) // Output: Node(value=5, next=Node(value=7, next=Node(value=9, next=Empty))))

// Test multiplyTwoLists() val multiplyList = exList.multiplyTwoLists(listOf(4, 5, 6).toLinkedList()) println(multiplyList) // Output: Node(value=4, next=Node(value=10, next=Node(value=18, next=Empty)))) }



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有